﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZ;
using DAL;
namespace SinhNhat.admin
{
    public partial class AdminCategory : System.Web.UI.Page
    {
        private static string RootID = "D5B39FE5-E7E9-4114-BD67-0D7CAD4C0C4A";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateRootCategories(0);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnAdd.Click += new ImageClickEventHandler(btnAdd_Click);
            btnDelete.Click += new ImageClickEventHandler(btnDelete_Click);
            btnEdit.Click += new ImageClickEventHandler(btnEdit_Click);
            btnUp.Click += new EventHandler(btnUp_Click);
            btnDown.Click += new EventHandler(btnDown_Click);
        }

        void btnEdit_Click(object sender, ImageClickEventArgs e)
        {
            if (lbCategory.SelectedIndex <= 0)
            {
                return;
            }

            Response.Redirect("/admin/EditCategory.aspx?id=" + lbCategory.SelectedValue);
        }

        void btnUp_Click(object sender, EventArgs e)
        {
            int index = lbCategory.SelectedIndex;
            int after = index - 1;

            if (index <= 0)
            {
                return;
            }

            string first = lbCategory.Items[index - 1].Text;
            if (!first.Contains("---"))
            {
                return;
            }

            string last = lbCategory.SelectedItem.Text;
            string firstID = string.Empty;

            if (last.Contains("---"))
            {
                firstID = lbCategory.Items[index - 1].Value;
            }
            else
            {
                for (int i = index - 1; i > 0; i--)
                {
                    if (!lbCategory.Items[i].Text.Contains("---"))
                    {
                        firstID = lbCategory.Items[i].Value;
                        after = index - (index - i);
                        break;
                    }
                }
            }

            Guid idLast = new Guid(lbCategory.SelectedValue);
            Guid idFirst = new Guid(firstID);
            CategoryBIZ categoryBIZ = new CategoryBIZ();
            categoryBIZ.OrderUp(idFirst, idLast);

            PopulateRootCategories(after);
        }

        void btnDown_Click(object sender, EventArgs e)
        {
            int index = lbCategory.SelectedIndex;
            int after = index + 1;

            if (index <= 0 || index + 1 >= lbCategory.Items.Count)
            {
                return;
            }

            string first = lbCategory.Items[lbCategory.SelectedIndex + 1].Text;
            string last = lbCategory.SelectedItem.Text;
            string firstID = string.Empty;


            if (last.Contains("---"))
            {
                if (!first.Contains("---"))
                {
                    return;
                }
                else
                {
                    firstID = lbCategory.Items[index + 1].Value;
                }
            }
            else
            {
                for (int i = index + 1; i < lbCategory.Items.Count; i++)
                {
                    if (!lbCategory.Items[i].Text.Contains("---"))
                    {
                        firstID = lbCategory.Items[i].Value;
                        after = index + (i - index);
                        break;
                    }
                }
            }

            Guid idLast = new Guid(lbCategory.SelectedValue);
            Guid idFirst = new Guid(firstID);
            CategoryBIZ categoryBIZ = new CategoryBIZ();
            categoryBIZ.OrderUp(idFirst, idLast);

            PopulateRootCategories(after);
        }

        void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            if (lbCategory.SelectedIndex <= 0)
            {
                return;
            }

            Guid categoryID = WebUtils.ToGuid(lbCategory.SelectedValue);
            if (categoryID.Equals(Guid.Empty))
            {
                return;
            }

            CategoryBIZ categoryBIZ = new CategoryBIZ();

            if (categoryBIZ.CheckCategoryCanDel(categoryID))
            {
                categoryBIZ.Delete(categoryID);
                PopulateRootCategories(0);
            }
            else
            {
                Response.Write("<script type=\"text/javascript\">alert('Category đang chưa Product. Không thể xoá.');</script>");
            }
        }

        private void PopulateRootCategories(int index)
        {
            lbCategory.Items.Clear();
            CategoryBIZ categoryBIZ = new CategoryBIZ();
            List<CATEGORY> listCategory = categoryBIZ.GetParentNode();
            listCategory.Insert(0, new CATEGORY() { CategoryID = new Guid(RootID), CategoryName = "Mục gốc" });
            List<CATEGORY> childList = null;

            for (int i = 0; i < listCategory.Count; i++)
            {
                childList = categoryBIZ.GetChildNode(listCategory[i].CategoryID.ToString());
                listCategory.InsertRange(i + 1, childList);
            }

            lbCategory.DataSource = listCategory;
            lbCategory.DataTextField = "CategoryName";
            lbCategory.DataValueField = "CategoryID";
            lbCategory.DataBind();

            lbCategory.SelectedIndex = index;

            up.Update();
        }

        void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (lbCategory.SelectedItem.Text.Contains("---"))
            {
                Response.Write("<script type=\"text/javascript\">alert('Chọn một Category khác');</script>");
                return;
            }

            CATEGORY category = new CATEGORY();
            category.CategoryName = txtNew.Text.Trim();
            category.Url = WebUtils.CorrectURL(txtNew.Text);

            //check url
            int i = 2;
            while (UrlBIZ.CheckPathExist(category.Url) != PathType.PhysicalPath)
            {
                category.Url = WebUtils.CorrectURL(txtNew.Text.Trim() + i);
                ++i;
            }

            if (lbCategory.SelectedValue.ToUpper().Equals(RootID))
            {
                Guid? guidNull = null;
                category.Parent = guidNull;
            }
            else
            {
                category.Parent = WebUtils.ToGuid(lbCategory.SelectedValue);
            }

            CategoryBIZ categoryBIZ = new CategoryBIZ();

            categoryBIZ.Add(category);
            Response.Write("<script type=\"text/javascript\">alert('Thêm thành công');</script>");

            PopulateRootCategories(0);
        }
    }
}